package com.teacher.hong.servlet;


import com.alibaba.fastjson.JSONObject;
import com.teacher.hong.dao.BaseDao;
import com.teacher.hong.dao.ClazzDao;
import com.teacher.hong.dao.UserDao;
import com.teacher.hong.entity.User;
import com.teacher.hong.utils.SecurityUtils;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;

@WebServlet("/getBean")
public class GetBeanServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /*
        在这里，尝试去Spring容器里取出我们想要BaseDao
         */
        //创建一个Bean工厂
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从Bean工厂中取出SpringBean
        BaseDao baseDao = beanFactory.getBean("baseDao", BaseDao.class);

        //这里，如果没有成功取出baseDao，此处调用.query()一定会报错
        ResultSet resultSet = null;
        try {
            resultSet = baseDao.query("select * from user");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        /**
         * 操作了一堆我们代码
         */
        JSONObject object = new JSONObject();
        object.put("state", 200);
        object.put("message", "success");
        object.put("value", resultSet.toString());
        object.put("timestamp", LocalDateTime.now());

        resp.setContentType("application/json");
        resp.getWriter().write(object.toJSONString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("getServletContext().getRealPath(\"/\"):" + getServletContext().getRealPath("/"));

        //创建一个应用上下文对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

//        BaseDao baseDao1 = context.getBean("baseDao", BaseDao.class);
////        baseDao1.setName("MySQL数据库");
//
//        BaseDao baseDao2 = context.getBean("baseDao", BaseDao.class);
//        BaseDao baseDao3 = context.getBean("baseDao", BaseDao.class);


        UserDao userDao = context.getBean("userDao", UserDao.class);
        int result = 0;
        try {
            result = userDao.selectByUser(new User("hong", SecurityUtils.md5Encrypt("456789")));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        ClazzDao clazzDao = context.getBean("clazzDao", ClazzDao.class);
        System.out.println("clazzDao:" + clazzDao);

        JSONObject object = new JSONObject();
        object.put("state", 200);
        object.put("message", "success");
        JSONObject value = new JSONObject();
        value.put("result", result);
        object.put("value", value);
        object.put("timestamp", LocalDateTime.now());

        resp.setContentType("application/json");
        resp.getWriter().write(object.toJSONString());
    }
}
